import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/login',
        component: () =>
            import ('@/views/login/index'),
        hidden: true
    },
    {
        path: '/register',
        component: () =>
            import ('@/views/register/index'),
        hidden: true
    },
    {
        path: '/404',
        component: () =>
            import ('@/views/404'),
        hidden: true
    },
    {
        path: '/',
        redirect: '/home/homeIndex'
            // ,
            // name: 'layout',
            // component: () =>
            //     import ('@/layout/index'),
            // children: [{
            //     path: '/index',
            //     name: 'Index',
            //     component: () =>
            //         import ('@/views/index/index')
            // }]
    },
    {
        path: '/home',
        redirect: 'homeIndex',
        name: 'home',
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'homeIndex',
            name: 'homeIndex',
            component: () =>
                import ('@/views/index/index'),
            meta: { title: '首页', icon: 'home' }
        }]
    },
    {
        path: '/competencyTest',
        redirect: 'competencyIndex',
        name: 'competencyTest',
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'competencyIndex',
            name: 'competencyIndex',
            component: () =>
                import ('@/views/competency/index'),
            meta: { title: '胜任力测试', icon: 'test' }
        }]
    },
    {
        path: '/result',
        redirect: 'resultIndex',
        name: 'result',
        component: () =>
            import ('@/layout/index'),
        children: [{
            path: 'resultIndex',
            name: 'resultIndex',
            component: () =>
                import ('@/views/result/index'),
            meta: { title: '测评结果', icon: 'result' }
        }]
    }
]

const blackList = [

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) { //跳转路由回到顶部
        for (let index = 0; index < blackList.length; index++) {
            const element = blackList[index];
            if (to.path.search(element) >= 0) {
                return;
            }
        }
        return { x: 0, y: 0 }
    }
})

const createRouter = () => new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) { //跳转路由回到顶部
        for (let index = 0; index < blackList.length; index++) {
            const element = blackList[index];
            if (to.path.search(element) >= 0) {
                return;
            }
        }
        return { x: 0, y: 0 }
    }
})

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router